local japi = require 'jass.japi'
local mt = require 'types.player'.__index
local jass = require 'jass.common'



function ac.hero_list(func)
    local list = {}
    for i = 1, 12 do
        local player = ac.player(i)
        local hero = player.hero
        if hero then
            table.insert(list,hero)
        end
    end
    return list
end


local base_name = {}
function mt:get_name()
    if not base_name[self.id] then
        local name = self.user_name
        if name=='' or name==nil then
            name = jass.GetPlayerName(self.handle)
        end
        base_name[self.id] = name:gsub("#%d+$",'')
    end
	return base_name[self.id]
end


ac.each_player(function (player)
    local uuid = japi.RequestExtraStringData(93, player.handle, nil, nil, false, 0, 0, 0)
    if package.local_test then
        player.uid = player:get_name()
        player.uuid = 'test_uuid'
    else
        --player.uid = player:get_kkid()
        player.uid = player:get_name()
    end
    
    ac.wait(100,function()
        player:notify('玩家-初始化算法', player)
    end)
end)

local unit_data = {}
for key,data in pairs(load_excel('单位数据.xlsx')) do
    for k,v in pairs(data) do
        unit_data[k] = v
    end
end

--创建模板单位
function mt:create_temp_unit(name,point,angle)
    local data = unit_data[name]
    if not data then
        print('没有单位模板',name)
        return unit
    end
    local temp = data['模板'] or 'Boss模板'
    local unit = self:create_unit(temp,point,angle)
    local spd = data['移动速度']
    local arp = data['攻击距离']
    for k,v in pairs(data) do
        unit[k] = v
    end
    unit.name = name
    unit.temp_name = name
    unit.title = name
    unit.art = data['图标']
    unit:set_size(data['模型大小'] or 1)
    unit:set_model(data['模型'])
    
    --技能
    if data['技能'] then
        local skills = split_string(data['技能'],',')
        if #skills>0 then
            for _,name in ipairs(skills) do
                unit:add_skill(name,'英雄')
            end
        else
            unit:add_skill(data['技能'],'英雄')
        end
    end

    --投射物修改
    -- local miss = data['投射物模型']
    -- if miss and unit._attack and not unit._attack.melee then
    --     unit._attack.missile_art = miss
    -- end

    --数值
    if spd then
        unit:set('移动速度',spd)
    end
    if arp then
        unit:set('攻击距离',arp)
    end

    unit:notify('单位-加载模板',unit)
    return unit
end